<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>childmatches.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_pattern_childmatches.js.html">childmatches.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview DOM pattern to match any children of a tag, and
<a name="line17"></a> * specifically collect those that match a child pattern.
<a name="line18"></a> *
<a name="line19"></a> * @author robbyw@google.com (Robby Walker)
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.dom.pattern.ChildMatches&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom.pattern.AllChildren&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.pattern.MatchType&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Pattern object that matches any nodes at or below the current tree depth.
<a name="line31"></a> *
<a name="line32"></a> * @param {goog.dom.pattern.AbstractPattern} childPattern Pattern to collect
<a name="line33"></a> *     child matches of.
<a name="line34"></a> * @param {number=} opt_minimumMatches Enforce a minimum nuber of matches.
<a name="line35"></a> *     Defaults to 0.
<a name="line36"></a> * @constructor
<a name="line37"></a> * @extends {goog.dom.pattern.AllChildren}
<a name="line38"></a> */
<a name="line39"></a>goog.dom.pattern.ChildMatches = function(childPattern, opt_minimumMatches) {
<a name="line40"></a>  this.childPattern_ = childPattern;
<a name="line41"></a>  this.matches = [];
<a name="line42"></a>  this.minimumMatches_ = opt_minimumMatches || 0;
<a name="line43"></a>  goog.dom.pattern.AllChildren.call(this);
<a name="line44"></a>};
<a name="line45"></a>goog.inherits(goog.dom.pattern.ChildMatches, goog.dom.pattern.AllChildren);
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Array of matched child nodes.
<a name="line50"></a> *
<a name="line51"></a> * @type {Array.&lt;Node&gt;}
<a name="line52"></a> */
<a name="line53"></a>goog.dom.pattern.ChildMatches.prototype.matches;
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Minimum number of matches.
<a name="line58"></a> *
<a name="line59"></a> * @type {number}
<a name="line60"></a> * @private
<a name="line61"></a> */
<a name="line62"></a>goog.dom.pattern.ChildMatches.prototype.minimumMatches_ = 0;
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * The child pattern to collect matches from.
<a name="line67"></a> *
<a name="line68"></a> * @type {goog.dom.pattern.AbstractPattern}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.dom.pattern.ChildMatches.prototype.childPattern_;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Whether the pattern has recently matched or failed to match and will need to
<a name="line76"></a> * be reset when starting a new round of matches.
<a name="line77"></a> *
<a name="line78"></a> * @type {boolean}
<a name="line79"></a> * @private
<a name="line80"></a> */
<a name="line81"></a>goog.dom.pattern.ChildMatches.prototype.needsReset_ = false;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Test whether the given token is on the same level.
<a name="line86"></a> *
<a name="line87"></a> * @param {Node} token Token to match against.
<a name="line88"></a> * @param {goog.dom.TagWalkType} type The type of token.
<a name="line89"></a> * @return {goog.dom.pattern.MatchType} {@code MATCHING} if the token is on the
<a name="line90"></a> *     same level or deeper and {@code BACKTRACK_MATCH} if not.
<a name="line91"></a> * @override
<a name="line92"></a> */
<a name="line93"></a>goog.dom.pattern.ChildMatches.prototype.matchToken = function(token, type) {
<a name="line94"></a>  // Defer resets so we maintain our matches array until the last possible time.
<a name="line95"></a>  if (this.needsReset_) {
<a name="line96"></a>    this.reset();
<a name="line97"></a>  }
<a name="line98"></a>
<a name="line99"></a>  // Call the super-method to ensure we stay in the child tree.
<a name="line100"></a>  var status =
<a name="line101"></a>      goog.dom.pattern.AllChildren.prototype.matchToken.apply(this, arguments);
<a name="line102"></a>
<a name="line103"></a>  switch (status) {
<a name="line104"></a>    case goog.dom.pattern.MatchType.MATCHING:
<a name="line105"></a>      var backtrack = false;
<a name="line106"></a>
<a name="line107"></a>      switch (this.childPattern_.matchToken(token, type)) {
<a name="line108"></a>        case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
<a name="line109"></a>          backtrack = true;
<a name="line110"></a>        case goog.dom.pattern.MatchType.MATCH:
<a name="line111"></a>          // Collect the match.
<a name="line112"></a>          this.matches.push(this.childPattern_.matchedNode);
<a name="line113"></a>          break;
<a name="line114"></a>
<a name="line115"></a>        default:
<a name="line116"></a>          // Keep trying if we haven&#39;t hit a terminal state.
<a name="line117"></a>          break;
<a name="line118"></a>      }
<a name="line119"></a>
<a name="line120"></a>      if (backtrack) {
<a name="line121"></a>        // The only interesting result is a MATCH, since BACKTRACK_MATCH means
<a name="line122"></a>        // we are hitting an infinite loop on something like a Repeat(0).
<a name="line123"></a>        if (this.childPattern_.matchToken(token, type) ==
<a name="line124"></a>            goog.dom.pattern.MatchType.MATCH) {
<a name="line125"></a>          this.matches.push(this.childPattern_.matchedNode);
<a name="line126"></a>        }
<a name="line127"></a>      }
<a name="line128"></a>      return goog.dom.pattern.MatchType.MATCHING;
<a name="line129"></a>
<a name="line130"></a>    case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
<a name="line131"></a>      // TODO(robbyw): this should return something like BACKTRACK_NO_MATCH
<a name="line132"></a>      // when we don&#39;t meet our minimum.
<a name="line133"></a>      this.needsReset_ = true;
<a name="line134"></a>      return (this.matches.length &gt;= this.minimumMatches_) ?
<a name="line135"></a>             goog.dom.pattern.MatchType.BACKTRACK_MATCH :
<a name="line136"></a>             goog.dom.pattern.MatchType.NO_MATCH;
<a name="line137"></a>
<a name="line138"></a>    default:
<a name="line139"></a>      this.needsReset_ = true;
<a name="line140"></a>      return status;
<a name="line141"></a>  }
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Reset any internal state this pattern keeps.
<a name="line147"></a> * @override
<a name="line148"></a> */
<a name="line149"></a>goog.dom.pattern.ChildMatches.prototype.reset = function() {
<a name="line150"></a>  this.needsReset_ = false;
<a name="line151"></a>  this.matches.length = 0;
<a name="line152"></a>  this.childPattern_.reset();
<a name="line153"></a>  goog.dom.pattern.AllChildren.prototype.reset.call(this);
<a name="line154"></a>};
</pre>


</body>
</html>
